<template>
  <div>
    <p style="text-align: center;">{{ courseName }}作业</p>
    <el-form :inline="true" size="small">
      <el-table :loading="loading" :data="tableData" style="width: 98%;" size="small" row-key="id">
        <el-table-column prop="studentName" label="姓名" />
        <el-table-column prop="studentId" label="学生id" />
        <el-table-column label="批改状态">
          <template #default="scope">
            <span :style="scope.row.num === scope.row.workNum ? 'color: green' : 'color: red'">
              {{ scope.row.num === scope.row.workNum ? '已批改' : '未批改' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="作业状态">
          <template #default="scope">
            <span :style="scope.row.correctNum === scope.row.workNum ? 'color: green' : 'color: red'">
              {{ scope.row.correctNum === scope.row.workNum ? '已完成' : '未完成' }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <div>
              <el-link type="primary" style="margin-right: 10px"
                :disabled="scope.row.correctNum !== scope.row.workNum || scope.row.num === scope.row.workNum"
                @click="goToStudentWork(scope.row)">
                查看
              </el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="demo-pagination-block" style="margin-top: 20px">
      <el-pagination size="small" v-model:current-page="page" v-model:page-size="size" :page-sizes="[5, 10, 15, 20]"
        :total="pagination.total" :background="true" layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
    </el-form>
    <WorkView v-if="updateDialog" v-model:updateDialog="updateDialog" @loadTableData="loadTableData" :rowData="rowData"
      :studentId="currentStudentId" :courseName="courseName" />
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import axios from "@/axios/index.js";
import { useRoute } from "vue-router";
import WorkView from "./WorkView.vue";

const route = useRoute();
const courseName = route.query.courseName;
const classId = route.query.classId;
let tableData = reactive([]);
let loading = ref(true);
let rowData = ref([]);
let updateDialog = ref(false);

const page = ref(1) //当前页
const size = ref(5) //每页显示条数
let pagination = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 5
})

let currentStudentId = ref(null);

function loadTableData() {
  loading.value = true;
  axios.get("courseHomeworkRecord/selectStudentWork", {
    params: {
      current: pagination.currentPage,
        size: pagination.pageSize,
        classId:classId,
      courseName: courseName,
    },
  }).then((resp) => {
    console.log(resp);
    
    loading.value = false;
    const data = resp.data.data;
    tableData = data.records;
    pagination.total = data.total;
  });
}

function handleCurrentChange(page) {
  pagination.currentPage = page;
  loadTableData();
}

function handleSizeChange(params) {
    pagination.pageSize=params
    loadTableData()
  }

function goToStudentWork(row) {
  rowData.value = row;
  updateDialog.value = true;
  currentStudentId.value = row.studentId;
}

loadTableData();
</script>

<style scoped></style>